package com.zhangjianling.premp.controller.sys;

import com.zhangjianling.premp.controller.base.BaseController;
import com.zhangjianling.premp.entity.sys.Dictionary;
import com.zhangjianling.premp.entity.sys.ResultEntity;
import com.zhangjianling.premp.server.sys.SysDictService;
import com.zhangjianling.premp.utils.SystemAopLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 字典信息
 * @author zhangjl
 *
 */
@RestController
@RequestMapping("sysDict")
public class SysDictController extends BaseController {
	@Resource
	private SysDictService sysDictService;

	/**
	 * 分页字典列表
	 * @return
	 */
	@RequiresPermissions("sysDict:dictList")
	@PostMapping("pageList")
	public ResultEntity pageList(@RequestParam(value = "pageCurrent", defaultValue = "1") String pageCurrent, Dictionary dict) {
		ResultEntity res = new ResultEntity();
		try {
			Page<Dictionary> dictPage = sysDictService.pageList(Integer.parseInt(pageCurrent), PAGESIZE, "code",dict);
			if (dictPage.getTotalPages() > 0) {
				res.setData(dictPage);
				res.setMessage(GET_SUCCESS);
				res.setCode(SUCCESS);
			} else {
				res.setMessage(IS_NULL);
				res.setCode(NULL);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 字典保存
	 * @param dict
	 * @return
	 */
	@PostMapping("saveDict")
	@SystemAopLog(description="字典保存")
	public ResultEntity saveDict(Dictionary dict,String parentDictid){
		ResultEntity res = new ResultEntity();
		try {
			if(StringUtils.isNotBlank(parentDictid)){
				Dictionary pdict = new  Dictionary();
				pdict.setDictId(parentDictid);
				dict.setParentDict(pdict);
			}
			sysDictService.saveDict(dict);
			res.setMessage(OPERATE_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(OPERATE_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	
	/**
	 * 删除字典
	 * @return
	 */
	@PostMapping("delDict")
	@SystemAopLog(description="删除字典")
	public ResultEntity delDict(String ID){
		ResultEntity res = new ResultEntity();
		try {
			sysDictService.deleteDict(ID);
			res.setMessage(DELETE_SUCCESS);
			res.setCode(SUCCESS);
		} catch (ConstraintViolationException e) {
			e.printStackTrace();
			res.setMessage("删除失败：没有删除与这条数据管理的相关数据");
			res.setCode(ERROR);
		} catch(Exception e){
			e.printStackTrace();
			res.setMessage(DELETE_ERROR+":"+e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 查询单个字典
	 * @return
	 */
	@GetMapping("findDict")
	public ResultEntity findDict(String ID){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysDictService.findDict(ID));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	

	/**
	 * 查询上级菜单
	 * @return
	 */
	@GetMapping("findAll")
	public ResultEntity findAll(){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysDictService.findAll());
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 根据父级主键查询子类
	 * @return
	 */
	@GetMapping("getChildres")
	public ResultEntity getChildres(String parentId){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysDictService.getChildres(parentId));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
}
